// @description: dashboard归类涉及所有面板数据相关的业务逻辑
//               其中，本文件涉及alerts告警数相关的
package ctrdashboard

import (
    "time"
    "fmt"
    "cloud_worm/util/umodels/mysqldb/dbdata"
    "cloud_worm/wormweb/controllers/ctrconfig"
    "cloud_worm/util/uhttp"
    "github.com/astaxie/beego/logs"
)

// @Description：dashboard alert 信息获取的Controller
type DbdAlertCntController struct {
    ctrconfig.LoginCtr
}

/**
 @Description：dashboard alert获取的信息
 @Param:
 @Return：
 */
func (d *DbdAlertCntController) Get()  {
    if d.Guest() == false {
        return
    }

    alertCnt := &AlertCountInfo{}

    // 参数解析
    inputType, err := d.GetInt("type", CountTypeWeek)
    if err != nil {
        logMsg := fmt.Sprintf("parse `type` error: %s", err.Error())
        logs.Error(logMsg)
        d.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, logMsg, alertCnt)
        d.ServeJSON()
        return
    }
    if inputType != CountTypeWeek && inputType != CountTypeMonth {
        logMsg := fmt.Sprintf("input `type` not '%d' and '%d', is '%d'", CountTypeWeek, CountTypeMonth, inputType)
        logs.Error(logMsg)
        d.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, logMsg, alertCnt)
        d.ServeJSON()
        return
    }

    // 设置time
    err = alertCnt.SetTime()
    if err != nil {
        logMsg := fmt.Sprintf("alert cnt settime error: %s", err.Error())
        logs.Error(logMsg)
        d.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, logMsg, alertCnt)
        d.ServeJSON()
        return
    }

    err = alertCnt.GetAllCheck()
    if err != nil {
        logs.Error(err.Error())
        d.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, err.Error(), alertCnt)
        d.ServeJSON()
        return
    }
    err = alertCnt.GetFailCheck()
    if err != nil {
        logs.Error(err.Error())
        d.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, err.Error(), alertCnt)
        d.ServeJSON()
        return
    }

    d.Data["json"] = uhttp.CreateOkResponse(alertCnt)
    d.ServeJSON()
    return
}

// @Description: 告警相关的警告信息
type AlertCountInfo struct {
    Type            int             // 月
    StartTime       int64           // 时间段开始点
    EndTime         int64           // 时间段截止点

    AllCheckAlert         int             // 所有的检查项
    AllCheckDuration      int             // 一段时间内的总检查项
    FailCheckAlert        int             // 检查不通过的数据，即告警项
    FailCheckDuration     int             // 一段时间内的失败检查项，即duration time的告警alert项
}

/**
 @Description：设置时间段
 @Param:
 @Return：
 */
func (a *AlertCountInfo) SetTime() error {
    a.EndTime = time.Now().Unix()
    dayDura, _ := time.ParseDuration("-24h")            // 提前一天的day duration

    if a.Type == CountTypeWeek {
        a.StartTime = time.Now().Add(7 * dayDura).Unix()          // 周报7天之前
    } else if a.Type == CountTypeMonth {
        a.StartTime = time.Now().Add(30 * dayDura).Unix()
    } else {
        return fmt.Errorf("count type error: %d", a.Type)
    }

    return nil
}

/**
 @Description：获取所有的检查项
 @Param:
 @Return：
 */
func (a *AlertCountInfo) GetAllCheck() error {
    w := &dbdata.WeaponAlertInfo{}
    tmpAllCkCnt, tmpAllCkDuration, err := w.AllCountByTimeDuration(a.StartTime, a.EndTime)
    if err != nil {
        return fmt.Errorf("get all check error: %s", err.Error())
    }

    a.AllCheckAlert = int(tmpAllCkCnt)
    a.AllCheckDuration = int(tmpAllCkDuration)
    return nil
}

/**
 @Description：获取失败的检查项
 @Param:
 @Return：
 */
func (a *AlertCountInfo) GetFailCheck() error {
    w := &dbdata.WeaponAlertInfo{}
    tmpFailCkCnt, tmpFailCkDuration, err := w.FailedCountByTimeDuration(a.StartTime, a.EndTime)
    if err != nil {
        return fmt.Errorf("get failed check error: %s", err.Error())
    }

    a.FailCheckAlert = int(tmpFailCkCnt)
    a.FailCheckDuration = int(tmpFailCkDuration)
    return nil
}